package version1_4;

import version1_1.GameJudge;
import version1_1.GameJudgeIMPH;
import version1_1.GestureTransition;
import version1_1.GestureTransitionIMPH;
import version1_3_1.GameFileOperate1;

import java.util.Scanner;
import java.util.concurrent.*;

/**
 * @program:IntelliJ IDEA
 * @Description:实现线程池对我们游戏线程进行管理
 * @author:Mr.CheCheHuiBujianO
 * @data: 2022/7/14 11:00
 */
public class ThreadPool {
    static int win = 0;//胜利局数
    static int beat = 0;//失败局数
    static int time = 0;//总局数
    public static void main(String[] args) {
        /*
        ThreadPoolExecutor参数
        1.核心线程
        2.最大线程数量
        3.存活时间
        4.存活时间单位
        5.任务队列
        6.线程创建工厂
        7.拒绝策略
         */
        ExecutorService pool = new ThreadPoolExecutor(4,5,6,
                TimeUnit.SECONDS,new ArrayBlockingQueue<>(5), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        int choose1 = -1;//选择角色进行我们的游戏（1.人 2.电脑）
        int choose2 = -1;//选择角色进行我们的游戏（1.人 2.电脑）
        System.out.println("选择角色进行我们的游戏（1.人 2.电脑）");
        Scanner choose_1 = new Scanner(System.in);
//        Scanner choose_2 = new Scanner(System.in);
        System.out.println("1.角色:");
        while (choose_1.hasNextInt()) {
            try {
                choose1 = choose_1.nextInt();
                System.out.println("2.角色:");
                choose2 = choose_1.nextInt();
                break;
            } catch (Exception e) {
                System.out.println("您的输入出现了问题！！！！");
                choose_1.nextLine();
//                choose_2.nextLine();
            }
        }
        FutureTask<String> people1 = new FutureTask<>(new People());
        FutureTask<String> people2 = new FutureTask<>(new People());
        FutureTask<String> computer1 = new FutureTask<>(new Computer());
        FutureTask<String> computer2 = new FutureTask<>(new Computer());
        //手势转化对象
        GestureTransition gestureTransition = new GestureTransitionIMPH();
        //游戏判断逻辑
        GameJudge gameJudge = new GameJudgeIMPH();
        //游戏文件操作
        GameFileOperate1 gameFileOperate1 = new GameFileOperate1();

        if (choose1 == 1 && choose2 == 1){
            //创建两个人线程,并将线程扔到线程池中
            while (win < 3 && beat < 3 && time < 5) {
                pool.submit(people1);
                pool.submit(people2);
                //游戏逻辑判断
                int gesture1 = -1;
                int gesture2 = -1;
                try {
                    //将人的手势的转化为数字
                    gesture1 = gestureTransition.gesture(people1.get());
                    gesture2 = gestureTransition.gesture(people2.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                //进行游戏的逻辑判断
                String gameJudge1 = gameJudge.gameJudge(gesture1, gesture2);
                switch(gameJudge1){
                    case "胜利":win++;time++;break;
                    case "失败":beat++;time++;break;
                    case "平局":time++;break;
                }
                System.out.println(gameJudge1);
                //将游戏结果写入我们结果文件中
                gameFileOperate1.createFileBuffer();
                gameFileOperate1.FileBufferWriter(gameJudge1);
            }
        }
        else if (choose1 == 2 && choose2 == 1){
            //创建一个人线程和一个电脑线程,并将线程扔到线程池中
            while (win < 3 && beat < 3 && time < 5) {
                pool.submit(computer1);
                pool.submit(people2);
                //游戏逻辑判断
                int gesture1 = -1;
                int gesture2 = -1;
                try {
                    //将人的手势的转化为数字
                    gesture1 = gestureTransition.gesture(computer1.get());
                    gesture2 = gestureTransition.gesture(people2.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                //进行游戏的逻辑判断
                String gameJudge1 = gameJudge.gameJudge(gesture1, gesture2);
                System.out.println(gameJudge1);
                switch(gameJudge1){
                    case "胜利":win++;time++;break;
                    case "失败":beat++;time++;break;
                    case "平局":time++;break;
                }
                //将游戏结果写入我们结果文件中
                gameFileOperate1.createFileBuffer();
                gameFileOperate1.FileBufferWriter(gameJudge1);
            }
        }else if (choose1 == 1 && choose2 == 2){
            while (win < 3 && beat < 3 && time < 5) {
                //创建一个人线程和一个电脑线程,并将线程扔到线程池中
                pool.submit(people1);
                pool.submit(computer1);
                //游戏逻辑判断
                int gesture1 = -1;
                int gesture2 = -1;
                try {
                    //将手势的转化为数字
                    gesture1 = gestureTransition.gesture(people1.get());
                    gesture2 = gestureTransition.gesture(computer1.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                //进行游戏的逻辑判断
                String gameJudge1 = gameJudge.gameJudge(gesture1, gesture2);
                System.out.println(gameJudge1);
                switch(gameJudge1){
                    case "胜利":win++;time++;break;
                    case "失败":beat++;time++;break;
                    case "平局":time++;break;
                }
                //将游戏结果写入我们结果文件中
                gameFileOperate1.createFileBuffer();
                gameFileOperate1.FileBufferWriter(gameJudge1);
            }
        }else if (choose1 == 2 && choose2 == 2){
            while (win < 3 && beat < 3 && time < 5) {
                //创建两个电脑线程,并将线程
                pool.submit(computer1);
                pool.submit(computer2);
                //游戏逻辑判断
                int gesture1 = -1;
                int gesture2 = -1;
                try {
                    //将人的手势的转化为数字
                    gesture1 = gestureTransition.gesture(computer1.get());
                    gesture2 = gestureTransition.gesture(computer2.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                //进行游戏的逻辑判断
                String gameJudge1 = gameJudge.gameJudge(gesture1, gesture2);
                System.out.println(gameJudge1);
                switch(gameJudge1){
                    case "胜利":win++;time++;break;
                    case "失败":beat++;time++;break;
                    case "平局":time++;break;
                }
                //将游戏结果写入我们结果文件中
                gameFileOperate1.createFileBuffer();
                gameFileOperate1.FileBufferWriter(gameJudge1);
            }
        }

    }
}
